import bean.TreeNode;

import java.util.LinkedList;

public class LeetCode1161 {
    public int maxLevelSum(TreeNode root) {
        int result = 1;
        int level = 1;
        int max = root.val;
        LinkedList<TreeNode> last = new LinkedList<>();

        last.add(root);
        while (!last.isEmpty() ){
            int sum = 0;
            LinkedList<TreeNode> current = new LinkedList<>();
            for (TreeNode node: last){
                sum+= node.val;
                if (node.left != null){
                    current.add(node.left);
                }

                if (node.right != null){
                    current.add(node.right);
                }
            }

            if (max < sum){
                result = level;
                max = sum;
            }

            level++;
            last = current;
        }

        return result;
    }
}
